package com.example.mkio.androidpractice.Views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.example.mkio.androidpractice.R;

import java.net.BindException;

/**
 * Created by mk.io on 17-6-2.
 */

public class CustomForth extends View {
    private static final String TAG = "CustomForth";

    private int mFirstColor;
    private int mSecondColor;
    private int mCircleWidth;

    private int mCurrentCount = 3;//当前进度
    private Bitmap mImage;
    private int mSplitSize;//每个块间的间隙
    private int mCount;//个数

    private Paint mPaint;
    private Rect mRect;


    public CustomForth(Context context) {
        this(context, null);
    }

    public CustomForth(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomForth(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomViewForth, defStyleAttr, 0);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.CustomViewForth_fFirstColor:
                    mFirstColor = typedArray.getColor(attr, Color.RED);
                    break;
                case R.styleable.CustomViewForth_fSecondColor:
                    mSecondColor = typedArray.getColor(attr, Color.BLUE);
                    break;
                case R.styleable.CustomViewForth_fCircleWidth:
                    mCircleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomViewForth_fDotCount:
                    mCount = typedArray.getInt(attr, 20);
                    break;
                case R.styleable.CustomViewForth_fSplitSize:
                    mSplitSize = typedArray.getInt(attr, 10);
                    break;
                case R.styleable.CustomViewForth_fBg:
                    mImage = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
                    break;
            }
        }
        typedArray.recycle();
        mPaint = new Paint();
        mRect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mCircleWidth);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        int center = getWidth() / 2;
        int radius = center - mCircleWidth / 2;

        drawOval(canvas, center, radius);

        int relRadius = radius - mCircleWidth / 2;
        mRect.left = (int) (relRadius * (1 - Math.sqrt(2) * 1.0f / 2)) + mCircleWidth;
        mRect.top = (int) (relRadius * (1 - Math.sqrt(2) * 1.0f / 2)) + mCircleWidth;
        mRect.right = (int) (mRect.left + Math.sqrt(2) * 1.0f * relRadius);
        mRect.bottom = (int) (mRect.left + Math.sqrt(2) * 1.0f * relRadius);
        Log.e(TAG, "onDraw: " + mRect.left + "-->" + mRect.top + "-->" + mRect.right + "-->" + mRect.bottom);
        if (mImage.getWidth() < Math.sqrt(2) * relRadius) {
            mRect.left = (int) (mRect.left + Math.sqrt(2) * 1.0f / 2 * relRadius - mImage.getWidth() / 2);
            mRect.top = (int) (mRect.top + Math.sqrt(2) * 1.0f / 2 * relRadius - mImage.getHeight() / 2);
            mRect.right = mRect.left + mImage.getWidth();
            mRect.bottom = mRect.top + mImage.getHeight();
            Log.e(TAG, "onDraw2: " + mRect.left + "-->" + mRect.top + "-->" + mRect.right + "-->" + mRect.bottom);


        }
        canvas.drawBitmap(mImage, null, mRect, mPaint);
    }

    private void drawOval(Canvas canvas, int center, int radius) {
        float itemSize = (360 * 1.0f - mCount * mSplitSize) / mCount;
        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
        mPaint.setColor(mFirstColor);
        for (int i = 0; i < mCount; i++) {
            canvas.drawArc(oval, (itemSize + mSplitSize) * i, itemSize, false, mPaint);
        }
        mPaint.setColor(mSecondColor);
        for (int i = 0; i < mCurrentCount; i++) {
            canvas.drawArc(oval, (itemSize + mSplitSize) * i, itemSize, false, mPaint);

        }
    }

    public void up() {
        mCurrentCount++;
        if(mCurrentCount>mCount)mCurrentCount=mCount;
        postInvalidate();
    }

    public void down() {
        mCurrentCount--;
        if(mCurrentCount<0)mCurrentCount=0;
        postInvalidate();
    }

    private int xDown, xUp;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                xDown = (int) event.getY();
                break;
            case MotionEvent.ACTION_UP:
                xUp = (int) event.getY();
                if (xUp > xDown) {
                    down();
                } else {
                    up();
                }
                break;
        }
        return true;
    }
}
